<?php namespace App\Presenters;

use App\Repositories\UserRepositoryEloquent;
use App\Transformers\UserTransformer;
use Prettus\Repository\Presenter\FractalPresenter;

/**
 * Class RolePresenter
 *
 * @package namespace App\Presenters;
 */
class UserPresenter extends FractalPresenter
{
    protected $user;

    public function __construct(UserRepositoryEloquent $user)
    {
        $this->user = $user;
    }

    /**
     * Transformer
     *
     * @return \League\Fractal\TransformerAbstract
     */
    public function getTransformer()
    {
        return new UserTransformer();
    }

    /**
     *  Roles checkbox
     * @param array $roles
     * @return string
     */
    public function parent($parent_id)
    {
        if($parent_id){
            $parent = $this->user->find($parent_id);
            if($parent){
                return $parent->name.'('.$parent->mobile.')';
            }
        }
        
        return '';
        
    }

    /**
     *  Roles checkbox
     * @param array $roles
     * @return string
     */
    public function rolesCheckbox($hasRoles = [])
    {
        $roles = $this->role->all();
        
        if(!$roles->count()) {
            return '<a class="btn btn-danger-alt btn-xs" href="'.route('admin.access.roles.create').'"> <i class="fa fa-hand-o-right"> 添加角色</i></a>';
        }
       
        $checkbox = '';
        foreach ($roles as $role) {
          
            if(in_array($role->slug,$hasRoles)) {
                $checkbox .= '<div class="checkbox block"><label><input type="checkbox" checked="checked"  name="roles[]" value="' . $role->id . '">' . $role->name . '</label></div>';
            } else {
                $checkbox .= '<div class="checkbox block"><label><input type="checkbox" name="roles[]" value="' . $role->id . '">' . $role->name . '</label></div>';
            }
        }

        return $checkbox;
    }

    /**
     *  Roles checkbox
     * @param array $roles
     * @return string
     */
    public function rolesSelect()
    {
        $roles = $this->role->all();
        
        if(!$roles->count()) {
            //return '<a class="btn btn-danger-alt btn-xs" href="'.route('admin.access.roles.create').'"> <i class="fa fa-hand-o-right"> 添加角色</i></a>';
        }

        $select = '<select name="roleid" class="form-control">';
        $select .= '<option value=0>所有角色</option>';
        foreach ($roles as $role) {
            if(isset($_GET['roleid']) && is_numeric($_GET['roleid']) && !empty($_GET['roleid'])){
                if($_GET['roleid'] == $role->id){
                    $select .= '<option selected value='.$role->id.'>'.$role->name.'</option>';
                }else{
                    $select .= '<option value='.$role->id.'>'.$role->name.'</option>';
                }
            }else{
                $select .= '<option value='.$role->id.'>'.$role->name.'</option>';
            }
            
            
        }
        $select .= '</select>';
        return $select;
    }

}
